﻿App.CutTrucker = function() {

    return {

        currentFormValues: {},

        getTopStore: function() {
            var topStore = new Ext.data.Store({
            	autoLoad:true,
				baseParams:{start:0, limit:10,flag:'jiecheguanli'},
				proxy: new Ext.data.HttpProxy({
					url : 'product/productlist_s'
				}),
                remoteSort: false,
                reader: new Ext.data.JsonReader({
					totalProperty : 'totalCount',
					root : 'rows',
					idProperty : 'logistics_id',
					fields: [{
						name: 'logistics_id',
						type: 'string'
					},
					{
						name: 'deal_id',
						allowBlank: false
					},
					{
						name: 'object_id',
						allowBlank: false
					},
					{
						name: "start_name",
						type: 'string'
					},
					{
						name: "end_name",
						type: 'string'
					},
					{
						name: "trucker",
						type: 'string'
					},
					{
						name: "trucker_phone",
						type: 'number'
					},
					{
						name: "truck_nbr",
						type: 'string'
					},
					{
						name: "sign_staff_name",
						type: 'string'
					},
					{
						name: "payout_staff_name",
						type: 'string'
					},
					{
						name: "sign_time",
						type: 'string'
//						dateFormat: 'time'
					},
					{
						name: "payout_time",
						type: 'string'
//						dateFormat: 'time'
					}]
				})
            });
            return topStore;
        },

        getForm: function() {
        	var locationStore = new Ext.data.Store({
            	autoLoad:false,
//				baseParams:{start:0, limit:23},
				proxy: new Ext.data.HttpProxy({
					url : 'localtion/localtionList'
				}),
                remoteSort: false,
                reader: new Ext.data.JsonReader({
					totalProperty : 'totalCount',
					root : 'rows',
					idProperty : 'local_id',
					fields: [{
						name: 'local_id',
						type: 'string'
					},
					{
						name: 'local_name',
						type: 'string'
					}]
				})
            });
        	var staffStore = new Ext.data.Store({
            	autoLoad:false,
//				baseParams:{start:0, limit:23},
				proxy: new Ext.data.HttpProxy({
					url : 'user/users'
				}),
                remoteSort: false,
                reader: new Ext.data.JsonReader({
					totalProperty : 'totalCount',
					root : 'rows',
					idProperty : 'staff_id',
					fields: [{
						name: 'staff_id',
						type: 'string'
					},
					{
						name: 'name',
						type: 'string'
					}]
				})
            });
            var frm = new Ext.form.FormPanel({
            	url:'cuttrucker/cut',
                labelAlign: 'left',
                buttonAlign: 'center',
                bodyStyle: 'padding:5px',
                frame: true,
                labelWidth: 80,
                defaultType: 'textfield',
                defaults: {
                    allowBlank: false,
                    anchor: '90%',
                    enableKeyEvents: true
                },
                items: [
                  {
                    xtype: 'hidden',
                    name: 'logistics_id',
                    value: ''
                },{
                	xtype: 'hidden',
                	name:"object_id",
                	value: ''
                },{
                	fieldLabel:"接车人",
            		xtype:'combo',
            		hiddenName :"cuttrucker_staff",
            		store: staffStore,
                    displayField:'name',
                    valueField :'staff_id',
                    mode: 'remote',
                    triggerAction: 'all',
                    editable: false,
                    emptyText:'选择接车人....',
                    listeners : {
                		expand:function(comb){
                			comb.getStore().reload();
                		}
                	}	
                },
                /*{
                	name:"cuttrucker_phone",
                	xtype:'numberfield',
                	fieldLabel:"截车人电话"
                },
               */ {
                	fieldLabel: '接车地点',
            		xtype:'combo',
            		hiddenName :"cuttrucker_location",
            		store: locationStore,
                    displayField:'local_name',
                    valueField :'local_id',
                    mode: 'remote',
                    triggerAction: 'all',
                    editable: false,
                    emptyText:'选择接车地点....',
                    listeners : {
                		expand:function(comb){
                			comb.getStore().reload();
                		}
                	}
                }
               ],
                //items
                buttons: [{
                    text: '保存',
                    scope: this,
                    handler: function() {
                       this.submit();
                    }
                },
                {
                    text: '重置',
                    scope: this,
                    handler: function() {
                        this.frm.form.reset();
                        this.frm.form.setValues(this.currentFormValues);
                        this.frm.form.clearInvalid();
                    }
                }] //buttons
            }); //FormPanel
            return frm;
        },

        submit: function() {
            if (this.frm.form.isValid()) {
				this.frm.getForm().submit({
                    waitTitle: '保存数据',
                    waitMsg: '正在保存……',
                    scope: this,
                    method: 'post',
                    params: '',
                    success: function(resp, opts) {
    					if(opts.result.success=='cutobj') {
    						Ext.Msg.alert('信息', opts.result.info);
    						this.dlg.hide();
    						this.lowerGrid.store.reload();
    					}else if(opts.result.success=='cutlog'){
    						Ext.Msg.alert('信息', opts.result.info);
    						this.dlg.hide();
    						this.lowerGrid.store.removeAll();
    						this.gird.store.reload();
    					}else {
                            Ext.Msg.alert('信息', opts.result.info);
                        }
    				},
    				failure: function(resp,opts) {
    					var result = Ext.util.JSON.decode(resp.responseText);
    					var info = result.info;
    					Ext.Msg.alert('提示', info); 
    				}
                });
            }
        },

        getDialog: function() {
        	return new Ext.Window({
                width: 400,
                height: 250,
                title: '接车',
                plain: true,
                closable: true,
                resizable: false,
                frame: true,
                layout: 'fit',
                closeAction: 'hide',
                border: false,
                modal: true,
                items: [this.frm],
                listeners: {
                    scope: this,
                    render: function(fp) {
                        this.frm.form.waitMsgTarget = fp.getEl();
                    },
                    show: function() {
                		this.frm.form.setValues(this.currentFormValues);
                        this.frm.form.clearInvalid();
                    }
                }
            }); //dlg
        },
        highLight: function(value) {
        	var key = App.Product.store.baseParams.query;
        	var v = value ? value.toString() : '';
        	if (Ext.isString(v) & Ext.isString(key)) {
        		if (key.length > 0) {
        			var re = new RegExp(key, "g");
        			return v.replace(re, "<span style='color:red;background-color:yellow;'>" + key + "</span>");
        		} else {
        			return v;
        		}
        	} else {
        		return v;
        	}
        },
        dateformate: function(value) {
        	return value.format("Y-m-d h:i:s");
        },
        createGrid: function(id) {

            var panel = Ext.getCmp(id);

            panel.body.dom.innerHTML = "";
            this.gird = this.getTopGrid();
            this.lowerGrid=this.getLowerGird();
            panel.add(this.gird);
            panel.add(this.lowerGrid);
        },
        getTopGrid: function (){
        	
        	var sm = new Ext.grid.RowSelectionModel({singleSelect:true});
            var topGrid = new Ext.grid.GridPanel({
                tbar: new Ext.PagingToolbar({
                    pageSize: 10,
                    displayInfo: true,
                    store: this.store,
                    items: ['-', 
	                    new Ext.ux.form.SearchField({
	                        store: this.store,
	                        width: 110
	                    }
                    )]
                }),
                title:"单号",//打印必须设置tile
                plugins: [Ext.ux.plugins.Print],//打印按钮
                loadMask: true,
                autoHeight:true,
                viewConfig : {autoFill:false,forceFit:true},
                id:'topGrid',
//                anchor:'top 50%',
                store: this.store,
                sm: sm,
                columns: [new Ext.grid.RowNumberer(),{
                    header: "单号",
                    sortable: true,
                    dataIndex: 'deal_id'
                },
                {
                    header: "起始地点",
                    sortable: true,
                    dataIndex: 'start_name'
//                    renderer: this.highLight(value,this.store)
                },
                {
                	header: "目的地点",
//                	width: 300,
                	sortable: true,
                	dataIndex: 'end_name'
//                	renderer: this.highLight(value,this.store)
                },
                {
                    header: "司机",
                    sortable: true,
                    dataIndex: 'trucker',
                    align: 'right'
                },
                {
                	header: "司机电话",
                	sortable: true,
                	dataIndex: 'trucker_phone',
                	align: 'right'
                },
                {
                    header: "车辆信息",
                    sortable: true,
                    dataIndex: 'truck_nbr',
                    align: 'right'
                },
                {
                	header: "签收人员",
                	sortable: true,
                	dataIndex: 'sign_staff_name'
//                	renderer: this.highLight(value,this.store)
                },
                {
                	header: "派发人员",
//                	width: 300,
                	sortable: true,
                	dataIndex: 'payout_staff_name'
//                	renderer: this.highLight(value,this.store)
                },
                {
                	header: "签收时间",
                	width: 130,
                	sortable: true,
                	dataIndex: 'sign_time'
//                	renderer: this.dateformate
                },
                {
                	header: "派发时间",
                	width: 130,
                	sortable: true,
                	dataIndex: 'payout_time'
//                	renderer: this.dateformate
                }],
                listeners: {
                    scope: this,
                    rowclick: function(grid,index) {
            			if(grid.getSelectionModel().hasSelection()){
	            			var logistics_id = grid.getSelectionModel().getSelected().data.logistics_id;
	            			this.lowerGrid.store.baseParams={start:0, limit:12,'logistics_id':logistics_id};
	            			this.lowerGrid.store.load();
            			}else{
            				this.lowerGrid.store.removeAll();
            			}
            		}
                }
            });
            return topGrid;
        },
        getLowerGird: function(){
        	var lowerStore = new Ext.data.Store({
                autoLoad:false,
				baseParams : {start:0, limit:12,stop:'fangzhishuaxin'},
				proxy: new Ext.data.HttpProxy({
					url : 'inhouse/items'
				}),
                remoteSort: false,
				reader: new Ext.data.JsonReader({
					totalProperty : 'totalCount',
					root : 'rows',
					idProperty : 'object_id',
					fields: [{
						name: 'object_id',
						type: 'string'
					},{
						name: 'objid',
						type: 'string'
					},{
						name:'obj_num',
						type:'number'
					},{
						name:'obj_name'
					},
					{
						name: 'from_addr',
						allowBlank: false
					},
					{
						name: 'to_addr',
						allowBlank: false
					},
					{
						name: 'from_name',
						allowBlank: false
					},
					{
						name: 'to_name',
						allowBlank: false
					},
					{
						name:'from_tell'
					},
					{
						name: "to_tell"
					},
					{
						name:"cost_type"
					},
					{
						name: "traff_fee"
					},
					{
						name: "mid_fee"
					},
					{
						name: "saf_fee"
					},
					{
						name: "oth_fee"
					},
					{
						name: "sum_fee",
						type:'double'
					},
					{
						name: "pre_fee"
					},
					{
						name: "inst_fee"
					},
					{
						name:"brief"
					},
					{
						name: "op_time",
						type: 'date',
						dateFormat: 'time'
					}]
				})
            });
        	var sm = new Ext.grid.CheckboxSelectionModel();
            var lowerGird = new Ext.grid.GridPanel({
            	autoHeight:true,
                loadMask: true,
                width:"120%",
                ref:"../lowerGird",
                tbar: new Ext.PagingToolbar({ 
                	pageSize: 12 ,//分页必须加的参数.最好和store里面的limit保持一致
                	displayInfo: true,
                    store: lowerStore,
                    items:[
                    {
	                    text: '整车整接',
	                    scope: this,
	                    handler: function(){
                    		this.cutLogis();
                    	}
	                },'-',
	                {
	                    text: '整车零接',
	                    scope: this,
	                    handler: function(){
	                		this.cutObj();
	                }
                },'-', new Ext.ux.form.SearchField({
                    store: lowerStore,
                    width: 110
                })]}),
                title:"上面所选单号对应的物件",//打印必须设置tile
                plugins: [Ext.ux.plugins.Print],//打印按钮
                viewConfig : {autoFill:false,forceFit:true},
                store: lowerStore,
                sm: sm,
                columns: [sm, new Ext.grid.RowNumberer(),
                  {
                    header: "货号",
                    width: 250,
                    sortable: true,
                    dataIndex: 'objid'
//                    renderer: this.highLight
                },
                {
                    header: "货名",
                    width: 250,
                    sortable: true,
                    dataIndex: 'obj_name'
//                    renderer: this.highLight
                },
                {
                    header: "件数",
                    width: 120,
                    sortable: true,
                    dataIndex: 'obj_num'
//                    renderer: this.highLight
                },
                {
                    header: "收货地",
                    width: 250,
                    sortable: true,
                    dataIndex: 'to_addr'
//                    renderer: this.highLight
                },
                {
                    header: "收货人",
                    width: 250,
                    sortable: true,
                    dataIndex: 'to_name'
//                    renderer: this.highLight
                },
                {
                    header: "收货人电话",
                    width: 250,
                    sortable: true
//                    dataIndex: 'to_tell'
                },
                {
                    header: "发货地",
                    width: 200,
                    sortable: true,
                    dataIndex: 'from_addr'
//                    renderer: this.highLight
                },
                {
                    header: "发货人电话",
                    width: 250,
                    sortable: true,
                    dataIndex: 'from_tell'
                },
                {
                    header: "付款方式",
                    width: 250,
                    sortable: true,
                    dataIndex: 'cost_type'
                },
                {
                    header: "运费",
                    width: 180,
                    sortable: true,
                    dataIndex: 'traff_fee'
                },
                {
                    header: "保险费",
                    width: 180,
                    sortable: true,
                    dataIndex: 'saf_fee'
                },
                {
                    header: "中间费",
                    width: 180,
                    sortable: true,
                    dataIndex: 'mid_fee'
                },
                {
                    header: "其它费",
                    width: 180,
                    sortable: true,
                    dataIndex: 'oth_fee'
                },
                {
                    header: "代收费",
                    width: 180,
                    sortable: true,
                    dataIndex: 'inst_fee'
                },
                {
                    header: "垫付费",
                    width: 180,
                    sortable: true,
                    dataIndex: 'pre_fee'
                },
                {
                    header: "汇总",
                    width: 180,
                    sortable: true,
                    dataIndex: 'sum_fee'
                },
       /*         {
                    header: "入库时间",
                    width: 250,
                    sortable: true,
                    dataIndex: 'op_time',
                    renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s')
                },
             */  {
                    header: "备注",
                    width: 400,
                    sortable: true,
                    dataIndex: 'brief'
                }]
            });
            return lowerGird;
        },
        
        cutLogis: function() {
        	  if (this.gird.getSelectionModel().hasSelection()) {
        		  var recs = this.gird.getSelectionModel().getSelected();
        		  Ext.apply(this.currentFormValues, {
              		logistics_id:recs.json.logistics_id,
              		object_id:"",
              		cuttrucker_staff:"",
              		cuttrucker_phone:"",
              		cuttrucker_location:""
        		  });
        		  this.dlg.show();
        	  }else{
        		  Ext.Msg.alert('信息', '请选择需要接车的单号！');
        	  }
        },
        cutObj: function() {
        	if (this.gird.getSelectionModel().hasSelection()) {
        		if (this.lowerGrid.getSelectionModel().hasSelection()) {
                    var recs = this.lowerGrid.getSelectionModel().getSelections();
                    var ids = [];
                    for(var i = 0; i < recs.length; i++) {
                        ids.push(recs[i].id);
                    }
                    Ext.apply(this.currentFormValues, {
                  		logistics_id:this.gird.getSelectionModel().getSelected().json.logistics_id,
                  		object_id:ids.toString(),
                  		cuttrucker_staff:"",
                  		cuttrucker_phone:"",
                  		cuttrucker_location:""
            		});
                    this.dlg.show();
                } else {
                    Ext.Msg.alert('信息', '请选择要接的货物！');
                }
        	 }else{
       		  Ext.Msg.alert('信息', '请选择要接货物对应的单号！');
       	  }
            
        },

        render: function(id) {
            if (!this.store) {
                this.store = this.getTopStore();
            }; //if(!this.store)
            if (!this.frm) {
                this.frm = this.getForm();
            }; //if(!this.frm)

            if (!this.dlg) {
                this.dlg = this.getDialog();
            }; //if(!this.dlg)
            this.createGrid(id);
        }

    }; //return
} (); //function
